<?php
N::loadClass('N_DB_Driver_Interface');

class N_DB_Driver_Mysql implements N_DB_Driver_Interface 
{
	protected $_connid;
	
	protected $_lastSql;
	
	/**
	 * 数据库用户名
	 *
	 * @var string
	 */
	private $_user = null;
	
	/**
	 * 数据库连接密码
	 *
	 * @var string
	 */
	private $_password = null;
	
	private $_host = null;
	
	private $_db = null;
	
	private $_debug = false;
	
	private $_resource;
	
	public function __construct()
	{
		$dsn = N::getIni('DSN');
		$this->parseDSN($dsn);
		$this->_connid = mysql_connect($this->_host, $this->_user, $this->_password);
		if ($this->_connid == false)
		{
			throw new Exception("数据库连接错误:".mysql_error());
		}
		$this->query('set names utf8');
		if (!empty($this->_db))
		{
			$this->selectDb($this->_db);
		}
		unset($this->_host);
		unset($this->_user);
		unset($this->_password);
		unset($this->_db);
	}
	
	/**
	 * 解析数组形式的数据库连接选项
	 *
	 * @param array $dsnArray
	 */
	public function parseDSN($dsnArray)
	{
		$host = null;
		$db = null;
		if (!empty($dsnArray['host']))
		{
			$this->_host = $dsnArray['host'];
		}
		if (!empty($dsnArray['db']))
		{
			$this->_db = $dsnArray['db'];
		}
		if (!empty($dsnArray['user']))
		{
			$this->_user = $dsnArray['user'];
		}
		if (!empty($dsnArray['password']))
		{
			$this->_password = $dsnArray['password'];
		}
	}
	
	
	public function query($sql)
	{
		$this->_lastSql = $sql;
		$rtn = mysql_query($sql, $this->_connid);
		if($this->_debug)
		{
			print '执行语句:'.$sql.'<br>';
			print '错误信息:'.$this->getErrorMsg().'<br>';
		}
		
		$this->_resource = $rtn;
		return $this;
	}
	
	public function exec($sql)
	{
		if($this->_debug)
		{
			print '执行语句:'.$sql.'<br>';
		}
		$this->_lastSql = $sql;
		if (mysql_query($sql, $this->_connid) === false)
		{
			throw new Exception($this->getErrorMsg());
		}
		return mysql_affected_rows($this->_connid);
	}

	public function fetch($mode = MYSQL_BOTH)
	{
		$rtn = array();
		if ($this->_resource == false)
		{
			return $rtn;
		}
		return mysql_fetch_array($this->_resource, $mode);
	}
	
	public function fetchRow()
	{
		$rtn = array();
		if ($this->_resource == false)
		{
			return $rtn;
		}
		return mysql_fetch_row($this->_resource);
	}
	
	public function fetchObject()
	{
		if ($this->_resource == false)
		{
			return null;
		}
		return mysql_fetch_object($this->_resource);
	}
	
	public function fetchAll($mode = MYSQL_BOTH)
	{
		$rtn = array();
		if ($this->_resource == false)
		{
			return $rtn;
		}
		while ($row = mysql_fetch_array($this->_resource, $mode)) 
		{
			array_push($rtn, $row);
		}
		return $rtn;
	}
	
	public function beginTransaction()
	{
		return $this->query('begin transaction;');;
	}
	
	public function rollBack()
	{
		return $this->query('roll back');
	}
	
	public function commit()
	{
		return $this->query('commit');
	}
	
	public function setDebug($debug = true)
	{
		$this->_debug = $debug;
	}
	
	public function selectDb($dbName)
	{
		return $this->query('use `'.$dbName.'`;');
	}
	
	public function getErrorMsg()
	{
		return mysql_errno().':'.mysql_error();
	}
}